package com.key1088.qianghongbao;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private CheckBox cbx, cbx4;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //final  Editable editText;

        cbx4 = (CheckBox) findViewById(R.id.checkBox4);
        cbx =  (CheckBox) findViewById(R.id.checkBox);

        if(PublicData.getB().equals("OK"))
            cbx4.setChecked(true);
        if(PublicData.getOldver().equals("OK"))
            cbx.setChecked(true);
        cbx4.setOnCheckedChangeListener(listener);
        cbx.setOnCheckedChangeListener(listener);
        final EditText editText=(EditText)findViewById(R.id.editText);
        final Button button = (Button)findViewById(R.id.button);
        editText.setText(PublicData.getC());
        findViewById(R.id.start_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                open();
            }
        });

        findViewById(R.id.website).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openweb();
            }
        });

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = Integer.parseInt(editText.getText().toString());
                if( i > 10) {
                    Toast.makeText(MainActivity.this, "轮训长度最大为10,不能保存", Toast.LENGTH_SHORT).show();
                    return;
                }
                PublicData.setC(editText.getText().toString());
                Toast.makeText(MainActivity.this, "配置已保存", Toast.LENGTH_SHORT).show();
            }
        });

    }


    private void open() {
        try {
            Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
            startActivity(intent);
            Toast.makeText(this, "找到抢红包，然后开启服务即可", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void openweb() {
        Uri uri = Uri.parse("http://www.iappedu.com");
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }

    CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox box = (CheckBox) buttonView;
            if(isChecked) {
                if (box.getText().equals("单群模式")) {
                    PublicData.setB("OK");
                }else if(box.getText().equals("微信老版本")) {
                    PublicData.setOldver("OK");
                }
            }else{
                if (box.getText().equals("单群模式")) {
                    PublicData.setB("KO");
                }else if(box.getText().equals("微信老版本")) {
                    PublicData.setOldver("KO");
                }
            }
        }
    };

}
